import { Module } from 'vuex'
import type { IRootState } from '../../types'
import { IDashBoardtype } from './types'
import {
  getAmountList,
  getaddressGoodsSale,
  getcategoryGoodsCount,
  getcategoryGoodsFavor,
  getcategoryGoodsSale
} from '@/service/main/analysis/dashboard'
const dashboardModule: Module<IDashBoardtype, IRootState> = {
  namespaced: true,
  state() {
    return {
      amountlist: [],
      categoryGoodsCount: [],
      addressGoodsSale: [],
      categoryGoodsSale: [],
      categoryGoodsFavor: []
    }
  },
  mutations: {
    changeaddressGoodsSale(state, list: any) {
      state.addressGoodsSale = list
    },
    changecategoryGoodsCount(state, list: any) {
      state.categoryGoodsCount = list
    },
    changecategoryGoodsFavor(state, list: any) {
      state.categoryGoodsFavor = list
    },
    changecategoryGoodsSale(state, list: any) {
      state.categoryGoodsSale = list
    },
    changeamountlist(state, list) {
      state.amountlist = list
    }
  },
  actions: {
    // 注意性能优化: 请求之后commit,在请求.
    // 而不是全请求完之后在commit
    async getdashboardDataAction({ commit }) {
      const amount = await getAmountList()
      const amountlist = amount.data
      commit('changeamountlist', amountlist)

      const addressGoodsSale = await getaddressGoodsSale()
      const addressGoodsSaleList = addressGoodsSale.data
      commit('changeaddressGoodsSale', addressGoodsSaleList)

      const categoryGoodsCount = await getcategoryGoodsCount()
      const categoryGoodsCountList = categoryGoodsCount.data
      commit('changecategoryGoodsCount', categoryGoodsCountList)

      const categoryGoodsFavor = await getcategoryGoodsFavor()
      const categoryGoodsFavorList = categoryGoodsFavor.data
      commit('changecategoryGoodsFavor', categoryGoodsFavorList)
      const categoryGoodsSale = await getcategoryGoodsSale()
      const categoryGoodsSaleList = categoryGoodsSale.data
      commit('changecategoryGoodsSale', categoryGoodsSaleList)
    }
  }
}
export default dashboardModule
